package cn.wolfcode.trip.admin.controller;


import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyDetailService strategyDetailService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", strategyDetailService.query(qo));
        model.addAttribute("strategies",strategyService.listAll());
        return "/strategyDetail/list";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(MultipartFile file , StrategyDetail entity) {
        if (file!=null&&file.getSize()>0){
            String upload = UploadUtil.upload(file, UploadUtil.PATH + "/upload");
            entity.setCoverUrl(upload);
        }
        strategyDetailService.saveOrUpdate(entity);
        return new JSONResult(); //象征性返回操作成功
    }

    @RequestMapping("getContentById")
    @ResponseBody
    public StrategyContent getContentById(Long id) {
        return strategyDetailService.getContentById(id);
    }
}
